package com.module.TraceSource.manager;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;

import com.module.TraceSource.dao.TsCodeForRecordDao;
import com.module.TraceSource.dao.TsKindDao;
import com.module.TraceSource.model.TraceSourceBeanUtil;
import com.module.TraceSource.util.ReturnUtil;
import com.module.TraceSource.vo.TsKind;

public class TsKindManager extends GenericHibernateManager<TsKind,Long>{

	@Override
	public GenericHibernateDao<TsKind, Long> getDao() {
		// TODO Auto-generated method stub
		return null;
	}
	private static  TsKindManager service;
	private TsKindDao dao = TsKindDao.getInstance();
	private TsCodeForRecordDao tsCodeForRecordDao = TsCodeForRecordDao.getInstance();
	public synchronized static TsKindManager getInstance(){
		if(service == null){
			service = new TsKindManager();
		}
		return service;
	}


	public TraceSourceBeanUtil listTsKindPage(TraceSourceBeanUtil bean)throws Exception{
		Session session=getSession();
		try {
			bean.setPage(dao.getList(session, bean.getTsKind(), bean.getPage().getStart(), bean.getPage().getLimit()));
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			this.closeSession();
		}
		return bean;
	}

	public TraceSourceBeanUtil saveTsKind(TraceSourceBeanUtil bean) throws Exception {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		TsKind kind = bean.getTsKind();
		String name = kind.getName().trim();
		boolean flag = false;
		try {
			flag=dao.isExist(session,name,kind.getId());
			if(flag){
				bean.getTsKind().setState("000001");
				kind.setName(name);
				flag = dao.save(session, kind);
				tx.commit();
				bean.setResult(ReturnUtil.optHandle(flag));
			}else{
				bean.setResult(ReturnUtil.optCodeErrorHandle("【"+name+"】已经存在,不允许重复插入数据"));
			}
		} catch (Exception e) {
			tx.rollback();
			throw e;
		} finally {
			this.closeSession();
		}
		return bean;
	}

	public  TraceSourceBeanUtil removeTsKindById(TraceSourceBeanUtil bean)throws Exception{
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		boolean flag = false;
		try {
			TsKind vo=dao.get(session, bean.getTsKind().getId());
			//查询从表中是否存在记录，如果存在就不能删除
			boolean isExist=tsCodeForRecordDao.isExist(session, bean.getTsKind().getId());
			if(!isExist){
				vo.setState("000002");
				flag=dao.update(session, vo);
				tx.commit();
				bean.setResult(ReturnUtil.optHandle(flag));
			}else{
				bean.setResult(ReturnUtil.optCodeErrorHandle(vo.getName()+"被引用,不能被删除"));
			}
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally {
			this.closeSession();
		}

		return bean;
	}

	public TraceSourceBeanUtil findTsKindById(TraceSourceBeanUtil bean)throws Exception{
		Session session = this.getSession();
		TsKind vo = new TsKind();
		try {
			vo=dao.get(session, bean.getTsKind().getId());
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			this.closeSession();
		}
		bean.setTsKind(vo);
		return bean;
	}

	public TraceSourceBeanUtil updateTsKind(TraceSourceBeanUtil bean)throws Exception{
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		boolean flag = false;
		try {
			flag=dao.update(session, bean.getTsKind());
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally {
			this.closeSession();
		}
		bean.setResult(ReturnUtil.optHandle(flag));
		return bean;
	}

	/**
	 * 获得所有的类别
	 * @return
	 * @throws Exception
	 */
	public List getAllTsKindName()throws Exception{
		Session session = this.getSession();
		List list=new ArrayList();
		try {
			list=dao.getAllTsKindName(session);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			this.closeSession();
		}
		return list;
	}
}
